package com.ddm.controller.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ddm.dto.webDTO.StandardDTO;
import com.ddm.result.PageResult;
import com.ddm.result.Result;
import com.ddm.service.webService.StandardFileService;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/Web/standardFile")
@Slf4j
@RestController("WebStandardFileController")
public class StandardFileController {
    @Autowired
    private StandardFileService standardFileService;

    @GetMapping("/findById")
    @ApiOperation("根据id查询")
    public Result<StandardDTO> getById(@ApiParam(value = "id", required = true)
                                  @RequestParam(required = true) Integer id) {
        log.info("根据id查询课程:{}", id);
        StandardDTO standardDTO = standardFileService.getStandardFileById(id);
        return Result.success(standardDTO);
    }

    @GetMapping("/pageAll")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public Result<PageResult> getAllStandardFileByPage(
            @ApiParam(value = "当前页码", required = false, defaultValue = "1")
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页记录数", required = false, defaultValue = "10")
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @ApiParam(value = "级数", required = false)
            @RequestParam(required = false) String level,
            @ApiParam(value = "关键词（模糊查询）", required = false)
            @RequestParam(required = false) String key

    ) {
        Page<StandardDTO> page = new Page<>(pageNum, pageSize);
        IPage<StandardDTO> coursePage = standardFileService.selectStandardByPage(page, level,key);

        // 使用 PageResult 封装分页信息
        PageResult pageResult = new PageResult(
                coursePage.getTotal(),      // 总记录数
                coursePage.getRecords()     // 当前页数据
        );

        return Result.success(pageResult);
    }

    @GetMapping("/pageByUserId")
    @ApiOperation("根据用户ID分页查询收藏的标准")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public Result<PageResult> getStandardFileByPageAndUser(
            //添加用户id
            @ApiParam(value = "用户id", defaultValue = "1")
            @RequestParam(required = true) Integer userId,
            @ApiParam(value = "当前页码", required = false, defaultValue = "1")
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页记录数", required = false, defaultValue = "10")
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @ApiParam(value = "关键词（模糊查询）", required = false)
            @RequestParam(required = false) String key)
    {
        Page<StandardDTO> page = new Page<>(pageNum, pageSize);
        // 调用新增的 service 方法，同时传入 userId 和 key
        IPage<StandardDTO> standardFilePage =standardFileService.getCollectedStandardFileByUserId(page, userId, key);

        // 使用 PageResult 封装分页信息
        PageResult pageResult = new PageResult(
                standardFilePage.getTotal(),      // 总记录数
                standardFilePage.getRecords()     // 当前页数据
        );

        return Result.success(pageResult);
    }

}
